package com.google.code.dhillon;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class ActivityUI extends ListActivity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		LostDbAdapter dbHelper = new LostDbAdapter(this);
		dbHelper.open();

		// fill data
		Cursor charCursor = dbHelper.fetchAllCharacters();
		startManagingCursor(charCursor); // let android take care of the memory
		// management

		// Create an array to specify the fields we want to display in the list
		String[] from = new String[] { LostDbAdapter.KEY_NAME, LostDbAdapter.KEY_DESC };

		// and an array of the fields we want to bind those fields to
		// note these are Android's ids, not ours since we are using Android's
		// list item layout
		int[] to = new int[] { android.R.id.text1, android.R.id.text2 };

		// Now create a simple cursor adapter and set it to display
		// we use the built in list item provided by android
		SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
				android.R.layout.simple_list_item_2, charCursor, from, to);
		setListAdapter(notes);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		// create the intent and pass any data it needs, in this case only row id
		Intent i = new Intent(this, CharacterBiography.class);
		i.putExtra(LostDbAdapter.KEY_ROWID, id);
		// startActivity instead of startActivityForResult since we don't want a result
		startActivity(i); 
	}
}